package com.heima.article.feign;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ApArticleService;
import com.heima.client.article.IArticleClient;
import com.heima.model.common.dtos.*;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.pojos.article.ApArticleConfig;
import com.heima.model.common.pojos.wemedia.WmNewsComment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author 陈辉
 * @data 2024 16:12
 */
@RestController
@Slf4j
public class ArticleClient implements IArticleClient {
    @Autowired
    private ApArticleService apArticleService;
    @Autowired
    private ApArticleConfigMapper configMapper;
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;





    /**
     * 保存或修改article文章信息
     *
     * @param articleDto
     * @return
     */
    @PostMapping("/api/v1/article/sync")
    public ResponseResult syncArticle(@RequestBody ArticleDto articleDto) {
        log.info("article文章同步开始了：{}....", articleDto);
       /* try {
            Thread.sleep(10000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        return apArticleService.saveOrUpdateArticle(articleDto);
    }

    /**
     * 加载文章列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult getArticleList(CommentPageDto dto) {
        log.info("fegin远程调用加载文章列表,入参={}",dto);
        log.info("fegin远程调用加载文章列表,入参={}", dto);

        return apArticleService.getArticleList(dto);
    }

    /**
     * 分页展示文章列表
     *
     * @param newsPageDto
     * @return
     */
    @PostMapping("/api/v1/statistics/newsPage")
    public ResponseResult newsPage(@RequestBody NewsPageDto newsPageDto) {
        return apArticleService.newsPage(newsPageDto);
    }

    /**
     * 获取文章配置信息
     * @return
     */
    @Override
    public ResponseResult getArticleConfig(Long articleId) {
        log.info("fegin远程调用获取文章配置信息,id={}",articleId);
        ApArticleConfig articleConfig = configMapper.selectOne(Wrappers.<ApArticleConfig>lambdaQuery()
                .eq(ApArticleConfig::getArticleId, articleId));
        return ResponseResult.okResult(articleConfig);
    }

    /**
     * 修改文章评论状态
     *
     * @param articleId
     * @param operation
     */
    @Override
    @PutMapping("/api/v1/article/update")
    public ResponseResult updateComment(@RequestParam Long articleId, @RequestParam Integer operation) {

        ApArticleConfig config = new ApArticleConfig();
        config.setArticleId(Long.valueOf(articleId));
        Boolean isComment = operation == 1;
        config.setIsComment(isComment);
        apArticleConfigMapper.update(config, Wrappers.<ApArticleConfig>lambdaUpdate()
                .eq(ApArticleConfig::getArticleId, articleId));
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    @PostMapping("/api/v1/article/query")
    public String selectNews(@RequestBody Map<String, String> params){
        log.info("远程调用，入参={}",params);
        String beginTime = params.get("beginTime");
        String endTime = params.get("endTime");
        CommentDto commentDto = new CommentDto();
        commentDto.setBeginTime(JSON.parseObject(beginTime,LocalDateTime.class));
        commentDto.setEndTime(JSON.parseObject(endTime,LocalDateTime.class));
        return  apArticleService.selectNews(commentDto);
    }

    /**
     * 清空评论数量
     *
     * @param commentDto
     */
    @Override
    public void deleteNews(CommentDto commentDto) {
        apArticleService.deleteNews(commentDto);
    }

    /**
     * 查询文章具体信息
     *
     * @param articleId
     * @return
     */
    @Override
    public ApArticleConfig selectArticle(Long articleId) {
        return apArticleConfigMapper.selectById(articleId);
    }

    /**
     * 添加评论数量
     *
     * @param entryId
     */
    @Override
    @PutMapping("/api/v1/statistics/save")
    public void saveComment(Long entryId) {
        apArticleService.saveComment(entryId);
    }
}
